<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

use App\Http\Common\NodeService;
use App\Http\Common\ToolsService;

class IndexController extends Controller
{


    public function index ()
    {
        // NodeService::applyAuthNode();
        $list = DB::table('system_menu')->where('status', '1')->get()->all();
        $menus = ToolsService::arr2tree($list);
        $title = '系统管理';
        // dd($menus);
        return view('admin.index',compact('title', 'menus'));
    }

    /**
     * 后台主菜单权限过滤
     * @param array $menus
     * @return array
     */
    private function _filterMenu($menus) {
        foreach ($menus as $key => &$menu) {
            if (!empty($menu['sub'])) {
                $menu['sub'] = $this->_filterMenu($menu['sub']);
            }
            if (!empty($menu['sub'])) {
                $menu['url'] = '#';
            } elseif (stripos($menu['url'], 'http') === 0) {
                continue;
            } elseif ($menu['url'] !== '#' && auth(join('/', array_slice(explode('/', $menu['url']), 0, 3)))) {
                
                $menu['url'] = url($menu['url']);
            } else {
                unset($menus[$key]);
            }
        }
        return $menus;
    }

    /**
     * 主机信息显示
     * @return View
     */
    public function main() {
        $mysql_ver = DB::select('select version() as ver')[0]->ver;
        if (Session::get('user')->username === 'admin' && Session::get('user')->password === '21232f297a57a5a743894a0e4a801fc3') {
            $url = url('admin/index/pass') . '?id=' . Session::get('user')->id;
            $alert = [
                'type'    => 'danger',
                'title'   => '安全提示',
                'content' => "超级管理员默认密码未修改，建议马上<a href='javascript:void(0)' data-modal='{$url}'>修改</a>！"
            ];
            $title = '后台首页';
        }
        return view('admin.main',compact('mysql_ver', 'alert', 'title'));
    }


    /**
     * 修改密码
     */
    public function pass(Request $req) {
        if (intval($req->id) !== intval(session('user')->id)) {
            return response()->json(['msg' => '访问异常！']);
        }
        if ($req->isMethod('get')) {
            $verify = true;
            $vo = DB::table('system_user')->where('id',$req->id)->first();
            return view('admin.pass',compact('verify','vo'));
        } else if ($req->isMethod('post')){
            if (in_array('10000', explode(',', $req->id))) {
                return response()->json(['msg' => '系统超级账号禁止操作！']);
            }
            //取所有POST数据
            $data = $req->all();
            //判断两次新密码是否一致
            if ($data['password'] !== $data['repassword']) {
                return response()->json(['msg' => '两次输入的密码不一致，请重新输入！']);
            }
            $user = DB::table('system_user')->where('id', session('user')->id)->first();
            //判断原密码
            
            if (md5($data['repassword']) !== $user->password) {
                return response()->json(['msg' => '旧密码验证失败，请重新输入！']);
            }

            if (DB::table('system_user')->where('id', Session::get('user')->id)->update(['password' => md5($data['password'])])) {
                return response()->json(['code' => 1, 'msg' => '密码修改成功，下次请使用新密码登录！']);
            } else {
                return response()->json(['msg' => '密码修改失败，请稍候再试！']);
            }
        }
    }
}
